<template>
  <el-button type="primary" @click="setPlatform" style="margin-bottom: 30px;">添加新的状态</el-button>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" border>
      <el-table-column label="状态名称" width="180">
        <template #default="scope">{{ scope.row.statuName }}</template>
      </el-table-column>
      <el-table-column label="添加时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="备注" width="180">
        <template #default="scope">
          <span class="headclass" style="display: block;width: 180px;height: 20px;"
            @click="setRemark(scope.row.remark, scope.row.id)">{{
    scope.row.remark
  }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否参与同步" width="180">
        <template #default="scope"><span class="headclass" style="width: 180px;"
            @click="syncStatusOr(scope.row.syncStatus, scope.row.id)">{{
    scope.row.syncStatus == 1 ? '同步' :
      '不同步'
  }}</span></template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="delectPlatformType(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4"
     :page-sizes="[10, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div> -->
  <!-- 添加 -->
  <div>
    <el-dialog v-model="dialogVisible" width="500">
      <span><el-form :model="form" label-width="auto" style="max-width: 600px">
          <el-form-item label="状态名称：" v-if="setOr !== '备注' && setOr !== '同步'">
            <el-input v-model="form.statuName" />
          </el-form-item>
          <el-form-item label="是否同步：" v-if="setOr !== '备注'">
            <div>
              <label>
                <input type="radio" :value="1" v-model="form.syncStatus"> 同步
              </label>
              <label>
                <input type="radio" :value="0" v-model="form.syncStatus"> 不同步
              </label>
            </div>
          </el-form-item>
          <el-form-item label="备注：" v-if="setOr !== '同步'">
            <el-input v-model="form.remark" type="textarea" />
          </el-form-item>
        </el-form></span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false, form = {}">取消</el-button>
          <el-button type="primary" @click="submitPlatformType">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { procurementAccountStatuscreate, procurementAccountStatuslist, procurementAccountStatusremove, procurementAccountStatusupdateSyncStatus, procurementAccountStatusupdateRemark } from '@/api/index'
import { ElMessage } from 'element-plus'
// do not use same name with ref
const form = ref({

})

const tableData = ref([])

onBeforeMount(() => {
  getList()
})
const getList = () => {
  procurementAccountStatuslist().then(res => {
    const { data: { data } } = res
    // total.value = data.total
    tableData.value = data
  })
}
// // 分页
// const currentPage4 = ref(1)
// const pageSize4 = ref(10)
// const total = ref(0)
// const handleSizeChange = (val) => {
//   console.log(`${val} items per page`)
//   form.value.pageSize = val
//   getList()
// }
// const handleCurrentChange = (val) => {
//   console.log(`current page: ${val}`)
//   form.value.pageNum = val
//   getList()
// }
// 添加
const dialogVisible = ref(false)
const setOr = ref('')
// const platformName = ref('')
const setPlatform = () => {
  setOr.value = '添加'
  dialogVisible.value = true
}
const submitPlatformType = () => {
  if (setOr.value === '添加') {
    procurementAccountStatuscreate(form.value).then(res => {
      dialogVisible.value = false
      ElMessage.success('添加成功')
      getList()
    })
  } else if (setOr.value === '备注') {
    procurementAccountStatusupdateRemark(ids.value, { remark: form.value.remark }).then(res => {
      dialogVisible.value = false
      ElMessage.success('添加成功')
      getList()
    })
  } else if (setOr.value === '同步') {
    procurementAccountStatusupdateSyncStatus(procurementAccountStatusId.value, { syncStatus: form.value.syncStatus }).then(res => {
      dialogVisible.value = false
      ElMessage.success('添加成功')
      getList()
    })
  }
}
// 删除
const delectPlatformType = (row) => {
  procurementAccountStatusremove(row.id).then(res => {
    ElMessage.success('删除成功')
    getList()
  })
}
// 修改备注
const ids = ref('')
const setRemark = (remark, id) => {
  ids.value = id
  setOr.value = '备注'
  dialogVisible.value = true
  form.value.remark = remark
}
// 状态
const procurementAccountStatusId = ref('')
const syncStatusOr = (status, id) => {
  procurementAccountStatusId.value = id
  setOr.value = '同步'
  dialogVisible.value = true
  form.value.syncStatus = status
}
</script>

<style lang='less' scoped></style>
